﻿using System;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace BikeBouncer.BusinessLayer.Entities
{
	[DataContract, Serializable]
	public class PoliceCollection : BaseCollection<Police>
	{
		#region Constructor

		public PoliceCollection()
		{
		}

		public PoliceCollection(SqlDataReader reader)
		{
			Load(reader);
		}

		#endregion

		#region Loading

		public override sealed void Load(SqlDataReader reader)
		{
			if (Count > 0)
			{
				Clear();
			}

			while (reader.Read())
			{
				Police p = new Police();
				p.Load(reader);

				Add(p);
			}

			if (!reader.IsClosed)
			{
				reader.Close();
			}

			reader.Dispose();
		}

		#endregion

		#region Filtering

		public PoliceCollection Filter(String keywords)
		{
			PoliceCollection places = new PoliceCollection();

			keywords = keywords.ToLower();

			foreach (Police p in this)
			{
				Address a = p.Address;

				if (p.Title.ToLower().Contains(keywords) ||
					a.Postcode.ToLower().Contains(keywords) ||
					a.Location.ToLower().Contains(keywords) ||
					a.City.Title.ToLower().Contains(keywords) ||
					a.City.Country.ToLower().Contains(keywords))
				{
					places.Add(p);
				}
			}

			return places;
		}

		#endregion
	}
}